 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
 // Jad home page: http://www.kpdus.com/jad.html
 // Decompiler options: packimports(3) 
 // Source File Name:   ListBean.java

 package com.jb.web.bean;

import com.jb.faces.context.FacesContext;
import com.jb.faces.el.AttributeEvaluator;
import com.jb.framework.PersistentContext;
import com.jb.framework.util.BeanSorter;
import com.jb.framework.util.ListSorter;
import com.jb.framework.util.PropUtil;
import com.jb.util.ClassUtil;
import com.jb.web.application.EventManager;
import com.jb.web.bean.inspector.OptionPropertyEditor;
import com.jb.web.context.WebContext;

import java.beans.PropertyDescriptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.el.ELException;

import javax.swing.ListModel;

import org.apache.commons.beanutils.PropertyUtils;


public class ListBean extends Grid
 {

     private void $init$()
     {
         list = Collections.EMPTY_LIST;
         _initialized = false;
         scope = "request";
         reverse = false;
         blockSize = 10;
         selected = -1;
         start = 0;
         _generateBinding = false;
     }

     public void setBeanClass(String beanClass)
     {
         this.beanClass = beanClass;
     }

     public String getBeanClass()
     {
         return beanClass;
     }

     public void setBlockSize(int blockSize)
     {
         this.blockSize = blockSize;
     }

     public int getBlockSize()
     {
         return blockSize;
     }

     public void setComparator(String comparator)
     {
         this.comparator = comparator;
     }

     public String getComparator()
     {
         return comparator;
     }

     public int getCurrentPage()
     {
         return start / (blockSize != 0 ? blockSize : 1);
     }

     public void setDefault(PersistentContext pCtx)
     {
         super.setDefault(pCtx);
         setStyleClass("jb_grid_table");
         setRows(1);
         valueChanged(pCtx);
     }

     public boolean isFirst()
     {
         return start == 0;
     }

     public int getFirstPage()
     {
         if(start != 0)
         {
             start = 0;
             setSelected(start);
         }
         return start;
     }

     public boolean isLast()
     {
         if(list != null)
             return start + blockSize >= list.size();
         else
             return true;
     }

     public int getLastPage()
     {
         if(blockSize == 0)
             blockSize = 10;
         int size = getSize();
         start = ((size - 1) / blockSize) * blockSize;
         setSelected(start);
         return start;
     }

     public void setList(List list)
     {
         if(this.list != list)
         {
             this.list = list;
             if(selected > list.size())
                 selected = 0;
         }
     }

     public List getList()
     {
         return list != null ? list : Collections.EMPTY_LIST;
     }

     public List getList(WebContext pCtx)
     {
         if(getValue() != null && (!_initialized || "request".equals(getScope())))
             try
             {
                 list = Collections.EMPTY_LIST;
                 _initialized = true;
                 Object obj = AttributeEvaluator.evaluate(pCtx, this, getValue(), Object.class);
                 if((obj instanceof List) && obj != list)
                 {
                     if(log.isDebugEnabled())
                         log.debug("Setting list");
                     setList((List)obj);
                 }
                 if(obj instanceof ListModel)
                     setModelBean(obj);
             }
             catch(ELException e)
             {
                 pCtx.addErrorMessage(this, (new StringBuilder()).append("Expression Error ").append(e.getMessage()).toString());
             }
         return getList();
     }

     public List getMetaData()
     {
         return PropUtil.getProperties(getClass());
     }

     public int getNext()
     {
         int i = selected + 1;
         if(i < getSize())
             setSelected(i);
         return selected;
     }

     public List getNextList()
     {
         List pageList = new ArrayList();
         int size = getSize();
         ListModel model = (ListModel)getModelBean();
         for(int i = Math.min(size, start + blockSize); i < start + 2 * blockSize && i < size; i++)
             if(model != null)
                 pageList.add(model.getElementAt(i));
             else
             if(list != null)
                 pageList.add(list.get(i));

         return pageList;
     }

     public int getNextPage()
     {
         if(start + blockSize < getSize())
         {
             start += blockSize;
             setSelected(start);
         } else
         {
             start = getFirstPage();
         }
         return start;
     }

     public void setOnSelectRow(String onSelectRow)
     {
         this.onSelectRow = onSelectRow;
     }

     public String getOnSelectRow()
     {
         return onSelectRow;
     }

     public void getPage(int i)
     {
         start = i * blockSize;
         if(start + blockSize < getSize())
             setSelected(start);
         else
             getLastPage();
     }

     public void setCurrentPage(int i)
     {
         getPage(i);
     }

     public List getPageList()
     {
         List pageList = new ArrayList();
         int size = getSize();
         for(int i = start; i < start + blockSize && i < size; i++)
             if(getModelBean() != null)
                 pageList.add(((ListModel)getModelBean()).getElementAt(i));
             else
             if(list != null)
                 pageList.add(list.get(i));

         return pageList;
     }

     public int getPages()
     {
         return getSize() / (blockSize != 0 ? blockSize : 1) + (getSize() % (blockSize != 0 ? blockSize : 1) <= 0 ? 0 : 1);
     }

     public int getPrevieous()
     {
         int i = selected - 1;
         if(i < 0)
             setSelected(0);
         return selected;
     }

     public List getPreviousList()
     {
         List pageList = new ArrayList();
         int size = getSize();
         ListModel model = (ListModel)getModelBean();
         for(int i = Math.max(0, start - blockSize); i < start && i < size; i++)
             if(model != null)
                 pageList.add(model.getElementAt(i));
             else
             if(list != null)
                 pageList.add(list.get(i));

         return pageList;
     }

     public int getPreviousPage()
     {
         if(start - blockSize >= 0)
         {
             start -= blockSize;
             setSelected(start);
         } else
         if(start != 0)
         {
             start = 0;
             setSelected(start);
         }
         return start;
     }

     public Object getRow()
     {
         return currentRow;
     }

     public Object getCurrentRow()
     {
         return currentRow;
     }

     public void setCurrentRow(Object rowSet)
     {
         currentRow = rowSet;
     }

     public int getRowIndex()
     {
         return list.indexOf(currentRow);
     }

     public void setScope(String scope)
     {
         this.scope = scope;
     }

     public String getScope()
     {
         return scope;
     }

     public void setSelected(int row)
     {
         row = start + row;
         if(selected > getSize())
         {
             return;
         } else
         {
             selected = row;
             processSelectLisnters();
             return;
         }
     }

     public Object getSelectedObject()
     {
         if(list != null && selected >= 0 && selected < list.size())
             return list.get(selected);
         else
             return null;
     }

     public int getSelected()
     {
         return selected;
     }

     public Object getSelectedItem()
     {
         if(selected < 0)
             return null;
         else
             return selected <= getSize() - 1 ? get(selected) : null;
     }

     public void setSelectedStyle(String selectedStyle)
     {
         this.selectedStyle = selectedStyle;
     }

     public String getSelectedStyle()
     {
         return selectedStyle;
     }

     public void setShowMetaData(boolean showMetaData)
     {
         this.showMetaData = showMetaData;
     }

     public boolean isShowMetaData()
     {
         return showMetaData;
     }

     public int getSize()
     {
         if(getModelBean() != null && (getModelBean() instanceof ListModel))
             return ((ListModel)getModelBean()).getSize();
         if(list != null)
             return list.size();
         else
             return 0;
     }

     public void setSortBy(String fieldName)
     {
         if(sortBy != null && sortBy.equals(fieldName))
         {
             reverse = reverse ^ true;
         } else
         {
             sortBy = fieldName;
             reverse = false;
         }
         sort();
     }

     public String getSortBy()
     {
         return sortBy;
     }

     public void setStart(int start)
     {
         this.start = start;
     }

     public int getStart()
     {
         return start;
     }

     public Object getThisRow()
     {
         return currentRow;
     }

     public Object get(int i)
     {
         if(getModelBean() != null)
             return ((ListModel)getModelBean()).getElementAt(i);
         if(list != null)
         {
             if(i < list.size())
                 return list.get(i);
             else
                 return null;
         } else
         {
             return null;
         }
     }

     public void goPage(int pPage)
     {
         start = pPage * blockSize;
         setSelected(start);
     }

     public void processUpdates(FacesContext pCtx)
     {
         WebContext ctx = (WebContext)pCtx;
         if(isMyEvent(ctx))
         {
             String row = ctx.getRequest().getParameter("row");
             if(row != null)
             {
                 int rowNo = Integer.valueOf(row).intValue();
                 setSelected(rowNo);
             }
         }
         super.processUpdates(pCtx);
     }

     public void remove(Object object)
     {
         if(!list.isEmpty())
             list.remove(object);
     }

     public void sort()
     {
         if(sortBy == null || sortBy.length() == 0)
             return;
         ListSorter sortor = null;
         if(comparator != null && comparator.length() > 0)
             try
             {
                 Class clazz = Thread.currentThread().getContextClassLoader().loadClass(comparator);
                 sortor = (ListSorter)clazz.newInstance();
             }
             catch(Exception e)
             {
                 if(log.isErrorEnabled())
                     log.error(this, e);
             }
         else
             sortor = new BeanSorter(sortBy) {

                 public int compare(Object o1, Object o2)
                 {
                     if(o1 == null || o2 == null)
                         return 0;
                     try
                     {
                         Object v1 = PropertyUtils.getProperty(o1, ListBean.ra$sortBy(ListBean.this));
                         Object v2 = PropertyUtils.getProperty(o2, ListBean.ra$sortBy(ListBean.this));
                         int i = super.compare(v1, v2);
                         return i;
                     }
                     catch(Exception e)
                     {
                         if(log.isErrorEnabled())
                             log.error(this, e);
                     }
                     return 0;
                 };
             }
 ;
         if(sortor != null)
         {
             sortor.setProperty(sortBy);
             sortor.setRevers(isReverse());
             Collections.sort(getList(), sortor);
         }
     }

     public void updateView(WebContext pCtx)
     {
         try
         {
             String visible = (String)AttributeEvaluator.evaluate(pCtx, this, getVisible(), String.class);
             if(!"true".equalsIgnoreCase(visible))
             {
                 super.updateView(pCtx);
                 return;
             }
             getList(pCtx);
             super.updateView(pCtx);
         }
         catch(Throwable e)
         {
             if(log.isErrorEnabled())
                 log.error(this, e);
             pCtx.addErrorMessage(this, e.getMessage());
         }
     }

     protected void processSelectLisnters()
     {
         if(onSelectRow != null && onSelectRow.length() > 0)
         {
             WebContext ctx = (WebContext)getFacesContext();
             if(ctx != null)
                 EventManager.getInstance().handleEvent(ctx, this, onSelectRow);
         }
     }

     public void setHighlightStyle(String highlightStyle)
     {
         this.highlightStyle = highlightStyle;
     }

     public String getHighlightStyle()
     {
         return highlightStyle;
     }

     public void setReverse(boolean reverse)
     {
         this.reverse = reverse;
     }

     public boolean isReverse()
     {
         return reverse;
     }

     public void setGenerateBinding(boolean param)
     {
         _generateBinding = param;
         generateBinding = false;
     }

     public boolean isGenerateBinding()
     {
         return generateBinding;
     }

     private Class getSourceClazz()
     {
         return ClassUtil.getClass(getBeanClass());
     }

     public boolean valueChanged(PersistentContext pCtx)
     {
         if(_generateBinding)
             generateBinding((WebContext)pCtx);
         return super.valueChanged(pCtx);
     }

     protected void generateBinding(WebContext pCtx)
     {
         Class clazz = getSourceClazz();
         if(clazz == null)
         {
             pCtx.addErrorMessage(this, (new StringBuilder()).append("Class not found ").append(beanClass).toString());
             return;
         }
         _generateBinding = false;
         List properties = PropUtil.getGetters(clazz);
         setShowFooter(true);
         setRows(1);
         TableFooter footer = getFooter(pCtx);
         footer.setCols(1);
         FloatPanel p = new FloatPanel();
         TableCell fCell = footer.getCellAt(0);
         fCell.setColSpan(properties.size());
         fCell.clearChildren();
         p.setEmbedded("paginator");
         fCell.addChild(p);
         TableHeader titleRow = getHeader(pCtx);
         titleRow.setCols(properties.size());
         TableRow dataRow = getRow(0);
         dataRow.setCols(properties.size());
         super.valueChanged(pCtx);
         int i = 0;
         for(Iterator iter = properties.iterator(); iter.hasNext();)
         {
             PropertyDescriptor pi = (PropertyDescriptor)iter.next();
             TableCell tCell = titleRow.getCellAt(i);
             tCell.setStyleClass("jbHeaderCell");
             TableCell dCell = dataRow.getCellAt(i);
             dCell.setStyleClass("jbTableCell");
             if(tCell != null)
                 tCell.setValue(pi.getName());
             if(dCell != null)
                 dCell.setValue((new StringBuilder()).append("${").append(getClientId()).append(".currentRow.").append(pi.getName()).append("}").toString());
             i++;
         }

     }

     public ListBean()
     {
         $init$();
     }

     static String ra$sortBy(ListBean listbean)
     {
         return listbean.sortBy;
     }

     static final long serialVersionUID = 0x1e0f3L;
     private transient List list;
     private transient boolean _initialized;
     private Object currentRow;
     private String beanClass;
     private String comparator;
     private String onSelectRow;
     private String scope;
     private String selectedStyle;
     private String highlightStyle;
     private String sortBy;
     private boolean reverse;
     private boolean showMetaData;
     private int blockSize;
     private int selected;
     private int start;
     private boolean generateBinding;
     protected boolean _generateBinding;

     static 
     {
         publish(ListBean.class, "General", "scope", new OptionPropertyEditor("seeded:scope"));
         publish(ListBean.class, "Model", "editable");
         publish(ListBean.class, "Model", "generateBinding");
         publish(ListBean.class, "Style", "selectedStyle");
         publish(ListBean.class, "Style", "highlightStyle");
         publish(ListBean.class, "Event Handler", "onSelectRow");
         publish(ListBean.class, "Model", "beanClass");
         publish(ListBean.class, "Model", "blockSize");
         publish(ListBean.class, "Model", "defaultValue");
         publish(ListBean.class, "Model", "value");
         publish(ListBean.class, "Model", "sortBy");
     }
 }
